package com.wondersgroup.fjzlpt.tbxt.controller.rest.sxzztz;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.wondersgroup.fjzlpt.tbxt.beans.req.sxzztz.QueryPageReqBean;
import com.wondersgroup.fjzlpt.tbxt.beans.req.sxzztz.SaveDataReqBean;
import com.wondersgroup.fjzlpt.tbxt.beans.resp.BaseRespBean;
import com.wondersgroup.fjzlpt.tbxt.beans.resp.sxzztz.QueryCountRespBean;
import com.wondersgroup.fjzlpt.tbxt.beans.resp.sxzztz.QueryPageRespBean;
import com.wondersgroup.fjzlpt.tbxt.service.inter.sxzztz.SxzztzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;

/**
 * 双向转诊台账
 */
@RestController
@RequestMapping("/sxzztz")
@Api(value = "双向转诊台账标准接口",tags = "SxzztzRestController", description = "双向转诊台账API")
public class SxzztzRestController {

	@Autowired
	private SxzztzService sxzztzService;

	/**
	 * 保存或修改
	 * @param request
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "保存类型(add/update)",required = true, paramType = "query"),
			@ApiImplicitParam(name = "ZZBS", value = "转诊标识(1:转出/2.转入)",required = true, paramType = "query")
	})
	@ApiOperation(value = "保存或修改", notes = "保存或修改双向转诊台账", response = BaseRespBean.class)
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public JSONObject save(HttpServletRequest request, ArrayList<SaveDataReqBean> rows)
	{
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();

		Map map = RequestUtils.GetParameterMap(request);
		//由于字段名称不对应，所以一个一个加
		map.put("PTJGID",userInfo.getString("PTJGID"));
		map.put("JGMC",userInfo.getString("YLJGMC"));
		map.put("JGLB",userInfo.getString("WSJGLBDM"));
		map.put("XZQHDM",userInfo.getString("JGXZQHDM"));
		map.put("USERID",userInfo.getString("USERID"));
		map.put("USERNAME",userInfo.getString("XM"));
		map.put("ZT","1");
		return sxzztzService.save(map);
	}

	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询双向转诊台账", response = QueryPageRespBean.class)
	@RequestMapping(value = "/queryPage", method = RequestMethod.POST)
	public JSONObject queryPage(HttpServletRequest request, QueryPageReqBean queryPageReqBean)
	{
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();

		Map map = RequestUtils.GetParameterMap(request);
		//由于字段名称不对应，所以一个一个加
		map.put("PTJGID",userInfo.getString("PTJGID"));
		map.put("JGLB",userInfo.getString("WSJGLBDM"));
		map.put("XZQHDM",userInfo.getString("JGXZQHDM"));
		return sxzztzService.queryPage(map);
	}

	/**
	 * 根据主键ID删除数据（不可恢复）
	 * @param request
	 * @return
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ID", value = "台账ID" , required = true, paramType = "query")
	})
	@ApiOperation(value = "删除", notes = "删除双向转诊台账数据（不可恢复）", response = BaseRespBean.class)
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JSONObject delete(HttpServletRequest request)
	{
		Map map = RequestUtils.GetParameterMap(request);
		return sxzztzService.delete(map);
	}

	/**
	 * 下载Excel模板
	 * @param response
	 * @return
	 */
	@ApiImplicitParam(name="type",value="模板类型（zcModel:转出模板/zrModel:转入模板）",paramType = "query",required = true)
	@ApiOperation(value = "下载模板", notes = "下载Excel模板，用于上传数据时使用")
	@RequestMapping(value = "downExcelModel", method = RequestMethod.POST)
	public void downExcelModel(HttpServletRequest request, HttpServletResponse response){
		sxzztzService.downExcelModel(request, response);
	}

	/**
	 * 解析上传的Excel数据
	 * @param request
	 * @return
	 */
	@ApiImplicitParam(name="importType",value="导入类型（zcExcel:转出/zrExcel:转入）",paramType = "query")
	@ApiOperation(value = "解析Excel", notes = "上传Excel文件并解析返回文件数据")
	@RequestMapping(value = "parseExcalData", method = RequestMethod.POST)
	public String parseExcalData(@RequestParam(value="fileField", required = false) MultipartFile file, HttpServletRequest request){
		String str = sxzztzService.parseExcalData(file, request);
		return str;
	}

	/**
	 * 查询双向转诊页签数据总量
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "统计数量", notes = "统计转诊总数", response = QueryCountRespBean.class)
	@RequestMapping(value = "/queryCount", method = RequestMethod.POST)
	public JSONObject queryCount(HttpServletRequest request)
	{
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();

		Map map = RequestUtils.GetParameterMap(request);
		//由于字段名称不对应，所以一个一个加
		map.put("PTJGID",userInfo.getString("PTJGID"));
		map.put("JGLB",userInfo.getString("WSJGLBDM"));
		map.put("XZQHDM",userInfo.getString("JGXZQHDM"));
		return sxzztzService.queryCount(map);
	}

}
